package com.lancoo.answer.view.fragment.composition.continuation.sub;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.lancoo.answer.R;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.bean.ItemAsk;
import com.lancoo.answer.util.encodeUtils.TextUtils;
import com.lancoo.answer.widget.combinationView.PictureAnswerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2022/5/27 9:00
 * Description:
 */
public class ContinuationImageAnswerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private final Item item;
    private final int typeIndex,quesIndex,childIndex;

    public ContinuationImageAnswerAdapter(Item item,int typeIndex,int quesIndex,int childIndex) {
        this.item = item;
        this.typeIndex = typeIndex;
        this.quesIndex = quesIndex;
        this.childIndex = childIndex;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.ev_list_continuation_answer_image_item_layout, parent, false);
        return new VH(inflate,typeIndex,quesIndex,childIndex);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VH vh= (VH) holder;
        vh.initData(item,position);
    }

    @Override
    public int getItemCount() {
        return item.getItemAskList()==null?0:item.getItemAskList().size();
    }

    public static class VH extends RecyclerView.ViewHolder{

        private Item item;
        private ItemAsk itemAsk;
        private Item dataItem;
        private final int typeIndex,quesIndex,childIndex;

        public VH(@NonNull View itemView,int typeIndex,int quesIndex,int childIndex) {
            super(itemView);
            this.typeIndex = typeIndex;
            this.quesIndex = quesIndex;
            this.childIndex = childIndex;
        }

        private void initData(Item item,int position){
            this.item=item;
            if(item.getItemAskList()==null){
                return;
            }
            this.itemAsk=item.getItemAskList().get(position);
            if(itemAsk==null){
                return;
            }
            PictureAnswerView pictureAnswerView=itemView.findViewById(R.id.pav);
            dataItem = new Item();
            dataItem.setImgRecognitionTxt(itemAsk.getImgRecognitionTxt()+"");
            List<String> list=new ArrayList<>();
            if(!TextUtils.isEmpty(itemAsk.getImagePath())){
                list.add(itemAsk.getImagePath());
            }
            dataItem.setImagePathList(list);
            pictureAnswerView.setMaxSize(1);
            pictureAnswerView.setIndex(typeIndex,quesIndex,childIndex,0);
            pictureAnswerView.initData(dataItem,"");
            TextView tv_order = itemView.findViewById(R.id.tv_order);
            tv_order.setText(itemAsk.getAskTip());
            pictureAnswerView.setCallBack(new PictureAnswerView.PictureAnswerCallBack() {
                @Override
                public void onDeletCallBack(int position) {
                    itemAsk.setImagePath("");
                    updateAnswer();
                }

                @Override
                public void onImgListUpLoad(List<String> imgs) {
                    if(imgs!=null&&imgs.size()>0){
                        itemAsk.setImagePath(imgs.get(0));
                        updateAnswer();
                        dataItem.setImagePathList(imgs);
                        pictureAnswerView.updateImgAdapterData();
                    }
                }

                @Override
                public void onImgTxtUpdate(String content) {
                    itemAsk.setImgRecognitionTxt(content);
                    StringBuilder sb=new StringBuilder();
                    for (ItemAsk itemAsk:item.getItemAskList()){

                        if(!TextUtils.isEmpty(itemAsk.getImgRecognitionTxt())){
                            if(!TextUtils.isEmpty(sb.toString())){
                                sb.append(" ");
                            }
                            sb.append(itemAsk.getImgRecognitionTxt());
                        }
                    }
                    item.setImgRecognitionTxt(sb.toString());
                }
            });



        }



        private void updateAnswer(){
            if(item == null){
                return;
            }
            List<ItemAsk> itemAskList = item.getItemAskList();
            List<String> url=new ArrayList<>();
            item.setImagePathList(url);
            if(itemAskList==null){
                return;
            }
            for (int i = 0; i < itemAskList.size(); i++) {
                String imagePath = itemAsk.getImagePath();
                if(!TextUtils.isEmpty(imagePath)){
                    url.add(imagePath);
                }
            }
            item.setImagePathList(url);

        }
    }
}
